<?php
	/**
	 * MusicNet Alignment Tool: MNItem
	 *
	 * Copyright (c) 2010 Joe Lambert
	 * 
	 * Permission is hereby granted, free of charge, to any person obtaining
	 * a copy of this software and associated documentation files (the
	 * "Software"), to deal in the Software without restriction, including
	 * without limitation the rights to use, copy, modify, merge, publish,
	 * distribute, sublicense, and/or sell copies of the Software, and to
	 * permit persons to whom the Software is furnished to do so, subject to
	 * the following conditions:
	 * 
	 * The above copyright notice and this permission notice shall be
	 * included in all copies or substantial portions of the Software.
	 * 
	 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
	 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
	 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
	 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
	 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
	 *  
	 * @author Joe Lambert
	 * @version 1.0
	 * @copyright Joe Lambert, 23 August, 2010
	 **/
	
	class MNItem
	{
		private $id;
		private $label;
		private $metadata = array();
		
		// Indexes
		private $a2z;
		private $hash;
		
		/**
		 * Constructor
		 * @param string $id
		 * @param string $label
		 * @param mixed $metadata
		 * @return object
		 * @author Joe Lambert
		 **/

		public function __construct($id, $label, $metadata=array(), $prefix=null)
		{
			$this->id = empty($prefix) ? $id : $prefix."_".$id;
			$this->label = $label;
			$this->prefix = $prefix;
			
			// Convert from object to array if needed
			if(is_object($metadata))
			{
				foreach($metadata as $k => $v)
					$this->metadata[$k] = $v;
			}
			else if(is_array($metadata))
				$this->metadata = $metadata;
				
			// Create indexes;
			$convert = iconv("UTF-8", "ASCII//TRANSLIT//IGNORE", $this->label);	// Replace diacritics with a-z
			$this->hash = preg_replace('/[^a-z]/', '', strtolower($convert));
			
			$this->a2z = $this->hash[0];
		}
		
		/**
		 * Saves the object to the database
		 *
		 * @return void
		 * @author Joe Lambert
		 **/
		public function save()
		{
			// Create a row in the items table
			$query = sprintf("INSERT INTO items (id, label, hash, a2z) VALUES ('%s', _utf8'%s', '%s', '%s')",
				mysql_real_escape_string($this->id),
				mysql_real_escape_string($this->label),
				mysql_real_escape_string($this->hash),
				mysql_real_escape_string($this->a2z));
				
			$result = mysql_query($query);
			
			if(!$result)
				echo "ERROR inserting ".$this->id."\n";
			
			$meta_query = "INSERT INTO metadata (item_id, `key`, value) VALUES ";	

			$item_id = mysql_real_escape_string($this->id);
			$firstloop = true;
			foreach($this->metadata as $k=>$v)
			{
				// In musicspace there are possible instances where composers are used in multiple items
				if(is_object($v))
				{
					foreach($v as $k1=>$v1)
					{
						if(!$firstloop)
							$meta_query .= ", ";

						$meta_query .= $this->createSQL($item_id, $k1, $v1);
						
						$firstloop = false;
					}
				}
				else
				{
					if(!$firstloop)
						$meta_query .= ", ";

					$meta_query .= $this->createSQL($item_id, $k, $v);
				}
					
				$firstloop = false;
			}
			
			$result = mysql_query($meta_query);
			
			if(!$result)
				echo "ERROR inserting metadata for ".$this->id."\n";
		}
		
		private function createSQL($item_id, $k, $v)
		{
			if(is_array($v))
			{
				$v = implode(' // ', $v);
			}
					
			return sprintf("('$item_id', '%s', _utf8'%s')",
						mysql_real_escape_string($k),
						mysql_real_escape_string($v));
		}
	}
?>